using HCore.IOC.Beans.Factory.Attribute.Attr;
using HCore.IOC.Context.Event.Attr;
using HCore.Tests.Runtime.ShootingEditor2D.Scripts.Query;
using HCore.Tests.Runtime.ShootingEditor2D.Scripts.Support;
using HCore.Tests.Runtime.ShootingEditor2D.Scripts.System.GunSystem;
using HCore.Tests.Runtime.ShootingEditor2D.Scripts.System.PlayerSystem;
using HCore.Tests.Runtime.ShootingEditor2D.Scripts.System.StatSystem;

namespace HCore.Tests.Runtime.ShootingEditor2D.Scripts.ViewController.UI
{
    public class UIController : HCoreCommandMonoBehaviour
    {
        private int mBulletMaxCount;
        [Autowired] private IGunSystem mGunSystem;
        [Autowired] private IPlayerModel mPlayerModel;
        [Autowired] private IStatSystem mStatSystem;

        protected override void Awake()
        {
            base.Awake();
            mPlayerModel.Hp.Register(ActionHpChanged).Trigger();
            mStatSystem.KillCount.Register(ActionKillCountChanged).Trigger();

            var currentGun = mGunSystem.CurrentGun;
            currentGun.BulletCountInGun.Register(ActionBulletCountInChanged).Trigger();
            currentGun.BulletCountOutGun.Register(ActionBulletCountOutChanged).Trigger();
            currentGun.Name.Register(ActionNameChanged).Trigger();
            currentGun.GunState.Register(ActionGunStateChanged).Trigger();
        }

        private void Start()
        {
            mBulletMaxCount = SendQuery(new MaxBulletCountQuery());
            mGunSystem.CurrentGun.BulletCountInGun.Trigger();
        }

        protected override void OnDestroy()
        {
            var gun = mGunSystem.CurrentGun;
            gun.GunState.UnRegister(ActionGunStateChanged);
            gun.Name.UnRegister(ActionNameChanged);
            gun.BulletCountOutGun.UnRegister(ActionBulletCountOutChanged);
            gun.BulletCountInGun.UnRegister(ActionBulletCountInChanged);
            mStatSystem.KillCount.UnRegister(ActionKillCountChanged);
            mPlayerModel.Hp.UnRegister(ActionHpChanged);
            base.OnDestroy();
        }

        /// <summary>
        ///     响应捡枪事件
        /// </summary>
        /// <param name="event"></param>
        [EventListener]
        private void OnPickGunEvent(GunChangedEvent @event)
        {
            mBulletMaxCount = SendQuery(new MaxBulletCountQuery());
            mGunSystem.CurrentGun.BulletCountInGun.Trigger();
        }

        private void ActionGunStateChanged(GunState newValue, GunState _)
        {
            Get("枪械状态:Text").SetText($"{newValue}");
        }

        private void ActionNameChanged(string newValue, string _)
        {
            Get("枪械名字:Text").SetText($"{newValue}");
        }

        private void ActionBulletCountOutChanged(int newValue, int _)
        {
            Get("后备子弹:Text").SetText($"{newValue}");
        }

        private void ActionBulletCountInChanged(int newValue, int _)
        {
            Get("子弹:Text").SetText($"{newValue}/{mBulletMaxCount}");
        }

        private void ActionKillCountChanged(int newValue, int _)
        {
            Get("击杀次数:Text").SetText($"{newValue}");
        }

        private void ActionHpChanged(int newValue, int _)
        {
            Get("生命:Text").SetText($"{newValue}/3");
        }
    }
}